package linuxbox.bean;


import java.util.*;
import org.apache.log4j.Logger;

import linuxbox.exception.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

public class BeanDef
{
  protected static Logger logger = Logger.getLogger(BaseBean.class.getName());

  protected Map allFields = new HashMap();
  protected Map keyFields = new HashMap();
  protected Class objClass = null;
  protected String tableName;

  public BeanDef(String n, Class c)
  {
    tableName = n;
    objClass = c;
  }

  public void setTableName(String sName)
  {
    tableName = sName;
  }

  public Class getObjClass()
  {
    return objClass;
  }

  public void setObjClass(Class c)
  {
    objClass = c;
  }

  public String getTableName()
  {
    return tableName;
  }

  public Map getAllFields()
  {
    return allFields;
  }

  public Map getKeyFields()
  {
    return keyFields;
  }

  public DBField addKeyField(String name, int type, boolean myAllowNull, String defaultVal, String myDescrip)  throws LinuxboxException
  {
    return addField(name, type, true, myAllowNull, defaultVal, myDescrip);
  }

  public DBField addField(String name, int type, boolean myAllowNull, String defaultVal, String myDescrip)  throws LinuxboxException
  {
    return addField(name, type, false, myAllowNull, defaultVal, myDescrip);
  }

  protected DBField addField(String name, int type, boolean myIsKey, boolean myAllowNull, String defaultVal, String myDescrip)  throws LinuxboxException
  {
    try
    {
      DBField field = new DBField(name, type, myIsKey, myAllowNull, defaultVal, myDescrip);
      field.setupField(objClass.getField(name));
      allFields.put(name, field);

      //Save the key fields in keyFields
      if (myIsKey)
        keyFields.put(name, field);
      return field;
    }
    catch (Exception e)
    {
      String err = "Error add Field:" + name + " for table:" + tableName;
      logger.error(err, e);
      throw new LinuxboxException(err);
    }
  }
}